package core.io;

import utils.ClassUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by ZhaiGuaili on 2018/7/5.
 */
public class ClassPathResource implements Resource {

    private String path;
    private ClassLoader classLoader;

    public ClassPathResource(String path,ClassLoader classLoader){
        this.path = path;
        this.classLoader = classLoader != null ?classLoader: ClassUtils.getDefaultClassLoader();
    }

    public ClassPathResource(String path){
        this.path = path;
        this.classLoader = ClassUtils.getDefaultClassLoader();
    }





    @Override
    public InputStream getInputStream () throws IOException{

        InputStream in = classLoader.getResourceAsStream(path);

        if(in == null){
            throw new FileNotFoundException("file "+ path+" can not open");
        }
        return in;
    }

    @Override
    public String getDescribtion() {
        return null;
    }


}
